package com.cqh.servlet;

import com.cqh.bean.Grade;
import com.cqh.bean.Student;
import com.cqh.service.GradeService;
import com.cqh.service.StudentService;
import com.cqh.service.impl.GradeServiceImpl;
import com.cqh.service.impl.StudentServiceImpl;
import com.cqh.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 所有与学生相关的servlet的集合
 */
@WebServlet(urlPatterns = "/Educational/student/StudentServlet")
public class StudentServlet extends HttpServlet {
    /**
     * 调用此servlet默认为查询全部学员
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("addStu".equals(method)){
            addStu(req,resp);
        }else if ("findById".equals(method)){
            findById(req,resp);
        }else if ("updateStu".equals(method)){
            updateStu(req,resp);
        }else if ("deleteStu".equals(method)){
            deleteStu(req,resp);
        }else {
            getStu(req, resp);
        }
    }

    //新增学员
    protected void addStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收数据
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");

        //2.调取service
        StudentService studentService = new StudentServiceImpl();
        //  将参数封装到学生对象当中
        Student student = new Student();
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        //  调取方法
        int i = studentService.insertStudent(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('新增成功！');location.href='/Educational/student/StudentServlet'</script>");
        }else {
            writer.println("<script>alert('新增失败！');location.href='/Educational/student/getGradeList'</script>");
        }
    }

    //主键查询
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        String stuId = req.getParameter("stuId");
        //根据主键查询学生信息
        StudentService studentService = new StudentServiceImpl();
        Student student = studentService.findById(Integer.parseInt(stuId));

        //查询年级列表
        GradeService gradeService = new GradeServiceImpl();
        List<Grade> gradeList = gradeService.getGradeList();

        req.setAttribute("stu",student);
        req.setAttribute("gradeList",gradeList);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    //查询列表
    protected void getStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取参数
        //1.1模糊查询
        PageUtil pageUtil = new PageUtil();
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
        //1.2分页数据   limit 开始位置，显示条数
        String pageIndex = req.getParameter("pageIndex");
        //  如果没有传入pageIndex的值，则默认为查询第一页
        int index = pageIndex==null?1:Integer.parseInt(pageIndex);
        String pageSize = req.getParameter("pageSize");

        //2.调取service方法
        StudentService studentService = new StudentServiceImpl();
        int Sex = sex==null?-1:Integer.parseInt(sex);
        List<Student> studentList = studentService.getAllStudents(stuname,stuno,Sex,index,pageUtil.getPageSize());
        //  获取总页数 = 总条数%每页显示的条数>0?总条数/每页条数+1:总条数/每页显示条数
        int total = studentService.total(stuname,stuno,sex==null?-1:Integer.parseInt(sex)); //总条数
        pageUtil.setTotal(total);

        //3.跳转页面
        //如果后台给前台传递数据，是一定要在后台进行存值
        pageUtil.setDataList(studentList);
        pageUtil.setPageIndex(index);
        //存储模糊查询的条件
        req.setAttribute("stuname",stuname);
        req.setAttribute("stuno",stuno);
        req.setAttribute("sex",Sex);
        //存储分页数据
        req.setAttribute("p1",pageUtil);    //将分页数据存储到对象中，进行页面通信
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

    //修改学员
    protected void updateStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.参数接收
        String stuId = req.getParameter("stuId");
        System.out.println(stuId);
        String stuNo = req.getParameter("stuNo");
        String stuName = req.getParameter("stuName");
        String gid = req.getParameter("gid");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idNumber = req.getParameter("idNumber");
        System.out.println(idNumber);
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");

        //2.调取service
        //  封装数据到student对象
        StudentService studentService = new StudentServiceImpl();
        Student student = new Student();
        student.setStuId(Integer.parseInt(stuId));
        student.setStuNo(stuNo);
        student.setStuName(stuName);
        student.setGid(Integer.parseInt(gid));
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setAddress(address);
        student.setPolitics(politics);
        student.setIdNumber(idNumber);
        student.setProfession(profession);
        student.setIntroduction(introduction);
        //调取方法
        int i = studentService.updateStu(student);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('更新成功！');location.href='/Educational/student/StudentServlet'</script>");
        }else {
            writer.println("<script>alert('更新失败！');location.href='/Educational/student/StudentServlet?method=findById&stuId="+stuId+"'</script>");
        }
    }

    //删除学员
    protected void deleteStu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数
        String stuId = req.getParameter("stuId");
        //根据学生id删除学生信息
        StudentService studentService = new StudentServiceImpl();

        int i = studentService.delStu(stuId);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i>0){
            writer.println("<script>alert('删除成功！');location.href='/Educational/student/StudentServlet'</script>");
        }else {
            writer.println("<script>alert('删除失败！');location.href='/Educational/student/StudentServlet'</script>");
        }
    }
}
